package com.itheima.reggie.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;

    @Override
    public Cart save(Cart cartParam) {
        //1.查询当前菜品/套餐在数据表中是否存在（当前登录用户user_id）
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());
        wrapper.eq(cartParam.getDishId() != null, Cart::getDishId, cartParam.getDishId());//这两步骤是判断购物车串货来的是菜品还是套餐
        wrapper.eq(cartParam.getSetmealId() != null, Cart::getSetmealId, cartParam.getSetmealId());
        Cart cart = cartMapper.selectOne(wrapper);
        if (cart != null) {
            //2查到当前传过来的对象在数据库中的记录，这个判断说明之前已经购物车已经有过记录了，所有，我们只需要将number+1就可了，这属于更新
            cart.setNumber(cart.getNumber() + 1);
            cartMapper.updateById(cart);
            return cart;
        } else {
            //到这行说明传过来的是购物车之前没有记录的，
            cartParam.setUserId(UserHolder.get().getId());
            cartParam.setNumber(1);
            cartParam.setCreateTime(new Date());
            cartMapper.insert(cartParam);
            return cartParam;
        }


    }

    @Override
    public List<Cart> findList() {
        //设置查询条件
        //SELECT * FROM `shopping_cart` WHERE user_id = '1458310743471493121' ORDER BY create_time ASC
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());//user_id = ?
        wrapper.orderByAsc(Cart::getCreateTime);//ORDER BY create_time ASC

        //执行查询
        return cartMapper.selectList(wrapper);
    }

    @Override
    public void clean() {
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId,UserHolder.get().getId());
        cartMapper.delete(wrapper);

    }

    @Override
    public Cart update(Cart cartParam) {
        Long dishId = cartParam.getDishId();
        Long setmealId = cartParam.getSetmealId();
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId,UserHolder.get().getId());
        if (dishId!=null){
            wrapper.eq(Cart::getDishId,dishId);
        }
        if (setmealId!=null){
            wrapper.eq(Cart::getSetmealId,setmealId);
        }
        Cart cart = cartMapper.selectOne(wrapper);
        if (cart.getNumber()-1>0){
            cart.setNumber(cart.getNumber()-1);
            cartMapper.updateById(cart);
        }
        if (cart.getNumber()-1==0){
            cartMapper.delete(wrapper);
        }
        return cart;
    }


}
